// packageK/platform/platform.js
import { pie, bar, line } from '../lib/chart-options'
const app = getApp()
const dayjs = require("../lib/dayjs.min")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total_withdraw_amount: '-',
    frozen_amount: '-',
    withdraw_able_amount: '-',
    chart1Options: bar,
    chart2Options: bar,
    chart3Options: bar,
    chart4Options: pie,
    rank_business_list: [],
    rank_supplier_list: []
  },

  /**
   * 获取图表数据
   */
  getChartData: function(e) {
    const _this = this
    const { startDate, endDate} = e.detail
    const url = app.getNetAddresss('platformApplet.statistics.get-platform')
    const data = {
      begin_time: dayjs(startDate).format('YYYY-MM-DD'),
      end_time: dayjs(endDate).format('YYYY-MM-DD')
    }
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url,
      data,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const { total_withdraw_amount, frozen_amount, withdraw_able_amount, business_list, supplier_list, primary_class_list, refund_primary_class_list , rank_business_list, rank_supplier_list } = resData.data
          let { chart1Options, chart2Options, chart3Options, chart4Options } = _this.data
          chart1Options.title.text = "平台商家营业额"
          chart1Options.yAxis[0].data = business_list.map(item => item.name)
          chart1Options.series[0].data = business_list.map(item => item.amount)
          chart2Options.title.text = "平台供货商营业额"
          chart2Options.yAxis[0].data = supplier_list.map(item => item.name)
          chart2Options.series[0].data = supplier_list.map(item => item.amount)
          chart3Options.title.text = "平台自营产品销售额"
          chart3Options.yAxis[0].data = primary_class_list.map(item => item.name)
          chart3Options.series[0].data = primary_class_list.map(item => item.amount)
          chart4Options.title.text = "平台退货类型占比"
          chart4Options.series[0].data = refund_primary_class_list.map(item => {
            return {
              name: item.name,
              value: item.amount
            }
          })
          _this.setData({
            total_withdraw_amount: '¥' + total_withdraw_amount,
            frozen_amount: '¥' + frozen_amount,
            withdraw_able_amount: '¥' + withdraw_able_amount,
            chart1Options,
            chart2Options,
            chart3Options,
            chart4Options,
            rank_business_list,
            rank_supplier_list
          })
        } else {
          app.tips(resData.msg)
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
    // setTimeout(function() {
    //   let { chart1Options, chart2Options, chart3Options } = _this.data
    //   chart1Options.title.text = "图表一"
    //   chart1Options.series[0].data = [
    //     { value: Math.floor(Math.random() * (100 - 1)) + 1, name: '商户1' },
    //     { value: Math.floor(Math.random() * (100 - 1)) + 1, name: '商户2' },
    //     { value: Math.floor(Math.random() * (100 - 1)) + 1, name: '商户3' },
    //     { value: Math.floor(Math.random() * (100 - 1)) + 1, name: '商户4' },
    //     { value: Math.floor(Math.random() * (100 - 1)) + 1, name: '商户5' }
    //   ]
    //   chart2Options.title.text = "图表二"
    //   chart2Options.yAxis[0].data = ['商家1', '商家2', '商家3', '商家4', '商家5']
    //   chart2Options.series[0].data = [
    //     Math.floor(Math.random() * (500 - 50)) + 1,
    //     Math.floor(Math.random() * (500 - 50)) + 1,
    //     Math.floor(Math.random() * (500 - 50)) + 1,
    //     Math.floor(Math.random() * (500 - 50)) + 1,
    //     Math.floor(Math.random() * (500 - 50)) + 1
    //   ]
    //   chart3Options.title.text="图表三"
    //   chart3Options.legend.data = ['商家1', '商家2', '商家3']
    //   chart3Options.xAxis.data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    //   chart3Options.series = [{
    //     name: '商家1',
    //     type: 'line',
    //     smooth: true,
    //     data: [
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1
    //     ]
    //   },{
    //     name: '商家2',
    //     type: 'line',
    //     smooth: true,
    //     data: [
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1
    //     ]
    //   }, {
    //     name: '商家3',
    //     type: 'line',
    //     smooth: true,
    //     data: [
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1,
    //       Math.floor(Math.random() * (100 - 1)) + 1
    //     ]
    //   }]
      
    //   _this.setData({
    //     chart1Options,
    //     chart2Options,
    //     chart3Options
    //   })
    //   wx.hideLoading()
    // }, 1000)
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})